<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState">
        <a-form-item name="stdate" label="操作日期" class="w-80">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="ck" label="仓库" class="ml-4">
          <SelectWarehouse v-model:value="formState.ck" class="!w-64" multiple />
        </a-form-item>
        <a-form-item name="zjm" label="" class="ml-4">
          <a-input v-model:value="formState.zjm" class="w-64" placeholder="药品编号/名称/助记码/出库单号" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <a-space class="flex justify-end mb-2">
        <BaseExport type="药库" name="出库查询" code="000055" :search-params="formState">导出</BaseExport>
      </a-space>
      <BaseTable
        :loading="loading"
        column-code="000055"
        :extra-height="50"
        :data-source="tableData"
        bordered
        fill
        :pagination="pagination"
        :scroll="{ x: 2000 }"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex == 'no'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'ypmc'">
            <MedicineInfo :value="{ mc: record.ypmc, gg: record.gg, cj: record.cj }" />
          </template>
          <template v-if="column.dataIndex == 'action'">
            <a-button type="primary" ghost size="small" @click="onHandlevView()"> 查看单据 </a-button>
          </template>
        </template>
        <template #summary>
          <a-table-summary fixed>
            <a-table-summary-row>
              <template v-for="column in columnsData" :key="column.dataIndex">
                <a-table-summary-cell v-if="column.dataIndex === 'no'"> 合计 </a-table-summary-cell>
                <a-table-summary-cell v-else-if="!column.isHide">
                  <div :class="bottomTotal[column.dataIndex] ? 'text-right' : 'text-center'">{{ bottomTotal[column.dataIndex] || '-' }}</div>
                </a-table-summary-cell>
              </template>
            </a-table-summary-row>
          </a-table-summary>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>
<script setup>
import { onMounted, ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import { formatAmount } from '@/utils/format.js'
import SearchContainer from '@/components/SearchContainer'
// import SelectDepartment from '@/components/SelectDepartment'
import SelectWarehouse from '@/components/SelectWarehouse'

import BaseTable from '@/components/BaseTable'

const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  ck: [],
  zjm: ''
})
const formRef = ref(null)
const bottomTotal = ref({})
const { loading, tableData, getData, pagination } = useTable({
  url: '/drugStorage/psiform/getOutStoreList',
  immediate: true,
  params: formState.value,
  formatResult: (result) => {
    if (result && result.length > 0) {
      bottomTotal.value.cbje = formatAmount(result[0].cbjehj)
    } else {
      bottomTotal.value.cbje = '0.00'
    }
    return result
  }
})
//获取表头
const columnsData = ref([])
const getTableColumns = async () => {
  const { code, data } = await post('/common/util/getTableMappingCloumnList', { id: '000055' })
  if (code !== 0) return message.error(code)
  columnsData.value = data
}

onMounted(() => {
  getTableColumns()
})
</script>
